﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Linq;
using Nwsfeed;

namespace nwsfeed
{
	/// <summary>
	/// Interaction logic for AddFeedWindow.xaml
	/// </summary>
    public partial class ManageFeedsWindow : Window, INotifyPropertyChanged
	{
        private MainWindow mainWindow;
		
		private String windowTitle;
        public String WindowTitle { get { return windowTitle; } set { windowTitle = value; OnPropertyChanged("WindowTitle"); } }

		private int numSelected;
		public bool OpenAddFeedsWindow;
		
        public ObservableCollection<RSSFeed> Feeds
        {
            get
            {
                return mainWindow.App.Feeds;
            }
        }

        // UI Properties
		
		public ManageFeedsWindow()
		{
			this.InitializeComponent();
            this.init();
		}

        public ManageFeedsWindow(MainWindow mainWindow)
        {
            this.mainWindow = mainWindow;
            this.InitializeComponent();
			Loaded +=new System.Windows.RoutedEventHandler(ManageFeedsWnd_Loaded);
            init();
        }

        private void init()
        {
            this.DataContext = this;

            // init UI properties
			WindowTitle = "Manage Feeds";

        }

		protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
		

		private void xButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			this.Close();
		}

        private void ManageFeedsWnd_Closed(object sender, EventArgs e)
        {
			Feeds.ToList<RSSFeed>().ForEach(p => p.IsChecked = false);			
            mainWindow.activeDialog = null;
        }

        private void ManageFeedsWnd_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
        	var style = (Style)Resources["WindowStyleModal"];
            this.Style = style;
        }

        private void checkBox_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
        	numSelected++;
			
			if (numSelected == 1)
				WindowTitle = "Manage Feeds - " + numSelected + " feed selected";	
			else
				WindowTitle = "Manage Feeds - " + numSelected + " feeds selected";			
        }

        private void checkBox_Unchecked(object sender, System.Windows.RoutedEventArgs e)
        {
        	numSelected--;
			
			if (numSelected == 0)
				WindowTitle = "Manage Feeds";
			else if (numSelected == 1)
				WindowTitle = "Manage Feeds - " + numSelected + " feed selected";	
			else
				WindowTitle = "Manage Feeds - " + numSelected + " feeds selected";	
		
        }

        private void buttonDelete_Click(object sender, System.Windows.RoutedEventArgs e)
        {
           var checkedFeeds = Feeds.Where(feed => feed.IsChecked);
           checkedFeeds.ToList<RSSFeed>().ForEach(p => mainWindow.App.RemoveFeed(p));

           numSelected = 0;
           WindowTitle = "Manage Feeds";
        }

        private void buttonAdd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	OpenAddFeedsWindow = true;
			this.Close();
        }

        private void checkBoxAll_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
        	Feeds.ToList<RSSFeed>().ForEach(p => p.IsChecked = true);
        }

        private void ManageFeedsWnd_Activated(object sender, System.EventArgs e)
        {
        	mainWindow.activeDialog = this;
        }

        private void checkBoxAll_Unhecked(object sender, System.Windows.RoutedEventArgs e)
        {
        	Feeds.ToList<RSSFeed>().ForEach(p => p.IsChecked = false);
        }

}
}